package org.wiw.client.gui;

import org.wiw.client.MarkerPlaceStorage;
import org.wiw.client.model.Person;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;

public class FilterDialog extends AbstractDialogBox {

	private final FilterModel filterModel;

	public FilterDialog(FilterModel filterModel) {
		super("Set Filter");
		this.filterModel = filterModel;
	}
	
	protected void createContent() {
		MarkerPlaceStorage storage = filterModel.getStorage();
		for(final Person p:storage.getPersons()){
			CheckBox check = new CheckBox(p.getNickname() + " ("+storage.getPersonPlacesCount(p)+")");
			check.setValue(filterModel.isChecked(p));
			check.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
				public void onValueChange(ValueChangeEvent<Boolean> event) {
					filterModel.setChecked(p, event.getValue());
					filterModel.apply();
				}
			});
			add(check);
		}

		add(new Button("Close" , 
					new ClickHandler() { public void onClick(ClickEvent event) {
						hide(); 
					} }));
	}
}
